<?php
namespace App\Util;
/**
 * sql server 数据库操作类
 * @author Hezone
 * 2018-9-6 10:27:35
*/
use Illuminate\Http\Request;

use App\Util\SqlServerUtil;
use App\Services\OSS;
use App;
class Common{
    /**
     * 获取 图片地址
     * @date: 2019年4月18日 下午2:53:56
     * @author: yy
     */
	protected static  $code_map =[
			'200' => 'Success',
			'400' => 'Fail',
			'401' => 'Error',
			'402' => 'Update Fail',
			
			
			'450' => '用户名、密码不能为空',
			'452' => '账号不能为空',
			'453'=> '客户名称不能为空',
			'454' => '客户代码不能为空',
			'455' => '请选择生日日期',
			'456' => '用户名已存在',
			'457' => '用户名、密码错误',
			'458' => '该用户未授权',
			'459' => '该用户不能登录其他电脑',
			'460' => '该用户先登录设备再授权',
			'461' => '该用户没有模块权限',
			'462' => '请选择账套名称',
			
			'700'=>'上传xls模板数据列不一致',
			'701'=>'上传xls模板数据为空',
			'702'=>'上传文件失败',
			'703'=>'Add Fail',
			'704'=>'Add Success',
			'705'=>'Edit Fail',
			'706'=>'Edit Success',
			'707'=>'设计款式参数错误',
			'708'=>'Delete Fail',
			
			'800'=>'公司名 已存在',
			'801'=>'检测公司名 不存在',
			'802'=>'id 非法或为空',
			
	];
	
    static function numberGetpic($SqlServer,$num,$type =1){
        //货号
        $goodPic =[];
        if(isset($num) && !empty($num)){
            $sql ="select item_number,type,url from yd_goods_pic where item_number like '{$num}%' and type= {$type} order by id asc";
            $goodPic = SqlServerUtil::exec($SqlServer,$sql);
        }else{
            $goodPic=[];
        }
        if(count($goodPic)<1){
            //判断是石料 和 成品
            if($type == 1){
                //默认 成品 -1
                $goodPic = [
                    [
                        "item_number" => "BSC0009388",
                        "type" => 1,
                        'url'=>self::Http().$_SERVER['HTTP_HOST'].'/uploads/chengpin.jpg'
                    ],
                ];
            }else{
                //默认石料- 2 Common::fanyi('')
                $goodPic = [
                    [
                        "item_number" => "Sapphire-Pyriform",
                        "type" => 1,
                        'url'=> self::Http().$_SERVER['HTTP_HOST'].'/sl12345.jpg'
                    ],
                    
                ];
            }
        }else{
             if(count($goodPic)>5){
                //最多取 5张
                 $goodPic =array_slice($goodPic,0,5);
                 foreach ($goodPic as $k =>$v){
                     $goodPic[$k]['url'] =self::Http().$_SERVER['HTTP_HOST'].$v['url'];
                 }
            }else{
                foreach ($goodPic as $k =>$v){
                    $goodPic[$k]['url'] =self::Http().$_SERVER['HTTP_HOST'].$v['url'];
                }
            }
            
        }
        
        return $goodPic;
    }
    
	
	/**
	* https 判断
	* @date: 2019年4月18日 下午5:10:58
	* @author: yy
	*/    
  static  function Http(){
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        return $http_type;
    }
    
    /**
    * 图片URL 调用
    * @date: 2019年4月18日 下午5:20:04
    * @author: yy
    */
    static  function imgUrl($result,$SqlServer){
        if(count($result)<1) return [];
        foreach ($result as $k =>$v){
            //如果是成品
            if($v['type'] == 1){
//           $sql ="select item_number,type,url from yd_goods_pic where item_number like '{$v['item_number']}%' and type= 1 order by id asc";
//           dd($sql);
//           $good_Pic = SqlServerUtil::exec($SqlServer,$sql);
             //$result[$k]['img_url'] =$good_Pic;
                $result[$k]['img_url'] = join(',',array_column(Common::numberGetpic($SqlServer,$v['item_number'],$type =1),'url'));
            }
            //如果是 石料 翻译--暂时
            if($v['type'] == 2){
                $item_number = Common::fanyi($v['stone_cate']).'-' .Common::fanyi($v['shape']);
                $tt= SqlServerUtil::find($SqlServer,"yd_goods_pic",['item_number' => $item_number]);
               // var_dump($tt);
                if(count($tt)>0){
                    $result[$k]['img_url'] = Common::Http().$_SERVER['HTTP_HOST'].$tt['url'];
                }else{
                    $result[$k]['img_url'] = Common::Http().$_SERVER['HTTP_HOST'].'/uploads/'.'sl12345.jpg' ;
                }
              //  $result[$k]['img_url'] = self::Http().join(',',array_column(Common::numberGetpic($SqlServer,$v['item_number'],$type =2),'url'));
                
            }
        }
        return $result;
        
    }
        
    /**
    * 数组去重
    * @date: 2019年4月19日 上午10:59:32
    * @author: yy
    */
    function assoc_unique(&$arr, $key) {
        $tmp_arr = array();
        foreach ($arr as $k => $v) {
            if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                unset($arr[$k]);
            } else {
                $tmp_arr[] = $v[$key];
            }
        }
        sort($arr); //sort函数对数组进行排序
        return $arr;
    }
    
    
    /**
    * 交集
    * @date: 2019年4月19日 上午11:04:55
    * @author: yy
    */
   static function getIntersection($array){
        //获取去掉重复数据的数组
        $unique_arr=array_unique($array);
        //获取重复数据的数组
        $repeat_arr = array_diff_assoc($array,$unique_arr);
        return $repeat_arr;
    } 
    
    /**
    * 错误
    * @date: 2019年4月26日 下午3:40:12
    * @author: yy
    * @param: variable
    * @return:
    */
    static function errorResponse($code = '-1', $message = '')
    {
    	$message = empty($message) ? (isset(self::$code_map[$code]) ? self::$code_map[$code] : '请求错误') : $message;
    	$arr = array(
    			'code' => $code ? strval($code) : '-1',
    			'message' => $message
    	);
    	return $arr;
    }
    static function errorResponseHQ($code = '-1', $message = '')
    {
        $message = empty($message) ? (isset(self::$code_map[$code]) ? self::$code_map[$code] : '请求错误') : $message;
        $arr = array(
            'Error' => $code ? strval($code) : '-1',
            'Message' => $message
        );
        return $arr;
    }
    
    /**
    * 文件用途描述
    * @date: 2019年4月27日 下午1:46:08
    * @author: yy
    */
    static function successResponse($code = '200', $message = '请求成功',$result=[])
    {
    	$arr = array(
    			'code' => $code ,
    			'message' => $message,
    			'result' =>$result,
    	);
    	return $arr;
    }
    
    
    
    
    static  function imagesUrl($result,$SqlServer){
    	if(count($result)<1) return [];
    	if(count($result)>0 && $result){
    		foreach ($result as $k=>$v){
    			$result[$k]['img_url']=join(',',array_column(self::getpicture($v['FModel'],$SqlServer),'url'));
    			$result[$k]['img_url_1']=array_column(self::getpicture($v['FModel'],$SqlServer),'url');
    		}
    	}
    	return  $result?$result:[];
    }
    
    static  function getpicture($num,$SqlServer){
    	if(isset($num) && !empty($num)){
    		$num =$num.'_';
    		$sql ="select item_number,type,url from yd_goods_pic where item_number like '%{$num}%' order by id asc";
    		$goodPic = SqlServerUtil::exec($SqlServer,$sql);
    	}else{
    		$goodPic=[];
    	}
    	if(count($goodPic)<1){
    			$goodPic = [
    					[
    					"item_number" => "kuanshi0001",
    					'url'=>self::Http().$_SERVER['HTTP_HOST'].'/uploads/kuanshi0001.jpg'
    					],
    			];
    	
    	}else{
    		if(count($goodPic)>5){
    			//最多取 5张
    			$goodPic =array_slice($goodPic,0,5);
    			foreach ($goodPic as $k =>$v){
    				$goodPic[$k]['url'] =self::Http().$_SERVER['HTTP_HOST'].$v['url'];
    			}
    		}else{
    			foreach ($goodPic as $k =>$v){
    				$goodPic[$k]['url'] =self::Http().$_SERVER['HTTP_HOST'].$v['url'];
    			}
    		}
    		
    	}
    	
    	return $goodPic;
    	
    }
        
    static  function imagesUrlDesign($result,$SqlServer){
    	if(count($result)<1) return [];
    	if(count($result)>0 && $result){
    		foreach ($result as $k=>$v){
    			$result[$k]['img_url']=join(',',array_column(self::getpicture($v['title_no'],$SqlServer),'url'));
    			$result[$k]['img_url_1']=array_column(self::getpicture($v['title_no'],$SqlServer),'url');
    		}
    	}
    	return  $result?$result:[];
    }
    
    static  public function  getNumbers($SqlServer){
    	$no ='DS';
    	$val =1;
    	$key = SqlServerUtil::exec($SqlServer,"select name, value from yd_config where name='design'");;
    	if($key){$val =$key[0]['value'];}
    	
    	$str = str_pad($val,7,0,STR_PAD_LEFT);
    	$str = $no.$str;
    	
    	//更新流水号
    	SqlServerUtil::exec($SqlServer,"update yd_config set value = {$val}+1 where name='design'");
    	return $str;
    }
    
    static public function ip() {
    	//strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
    	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
    		$ip = getenv('HTTP_CLIENT_IP');
    	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
    		$ip = getenv('HTTP_X_FORWARDED_FOR');
    	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
    		$ip = $_SERVER['REMOTE_ADDR'];
    	}
    	
    	/* elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
    		$ip = getenv('REMOTE_ADDR');
    	}  */
    	$res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    	return  $res;
    	
    }
    
    
    static function object_array($array) {
    	if(is_object($array)) {
    		$array = (array)$array;
    	}
    	if(is_array($array)) {
    		foreach($array as $key=>$value) {
    			$array[$key] = self::object_array($value);
    		}
    	}
    	return $array;
    }
    
    
    static function get_real_ip(){
    	static $realip;
    	if(isset($_SERVER)){
    		if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
    			$realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    		}else if(isset($_SERVER['HTTP_CLIENT_IP'])){
    			$realip=$_SERVER['HTTP_CLIENT_IP'];
    		}else{
    			$realip=$_SERVER['REMOTE_ADDR'];
    		}
    	}else{
    		if(getenv('HTTP_X_FORWARDED_FOR')){
    			$realip=getenv('HTTP_X_FORWARDED_FOR');
    		}else if(getenv('HTTP_CLIENT_IP')){
    			$realip=getenv('HTTP_CLIENT_IP');
    		}else{
    			$realip=getenv('REMOTE_ADDR');
    		}
    	}
    	return $realip;
    }
        
   
}


